package com.ruoyi.bussiness.api;

import cn.hutool.json.JSONObject;
import com.ruoyi.common.core.api.ApiResult;
import com.ruoyi.common.core.domain.entity.AppWxParam;
import com.ruoyi.common.dto.LoginDTO;
import com.ruoyi.common.param.ProductParam;
import com.ruoyi.common.service.business.*;
import com.ruoyi.common.vo.business.LoginWxVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/ma")
@Slf4j
@Api(value = "微信小程序")
public class ApiMaController {

    @Autowired
    private MpUserService mpUserService;
    @Autowired
    private ProductCategoryService categoryService;
    @Autowired
    private ProductService productService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private ShoppingCartService cartService;



    @RequestMapping(value = "/login",method = RequestMethod.GET)
    @ApiOperation(value = "微信登录")
    public ApiResult<LoginWxVo> login(AppWxParam appWxParam){
        LoginWxVo vo= mpUserService.loginWx(appWxParam);
        return ApiResult.ok(vo);
    }
    @RequestMapping(value = "/changePwd",method = RequestMethod.POST)
    @ApiOperation(value = "修改密码")
    public ApiResult<String> changePwd(@RequestBody LoginDTO loginDTO){
        mpUserService.changePwd(loginDTO);
        return ApiResult.ok("修改成功");
    }

    @RequestMapping(value = "/checkPwdIsDefault",method = RequestMethod.POST)
    @ApiOperation(value = "修改密码")
    public ApiResult<String> checkPwdIsDefault(@RequestBody LoginDTO loginDTO){
        mpUserService.checkPwdIsDefault(loginDTO);
        return ApiResult.ok("修改成功");
    }
    @RequestMapping(value = "/index",method = RequestMethod.GET)
    @ApiOperation(value = "首页")
    public ApiResult<JSONObject> index(){
        JSONObject result = new JSONObject();
        //分类
        result.set("categoryList",categoryService.treeList());
        //当前分类产品
        result.set("productList",productService.pageMaProduct(new ProductParam()));
        //购物车数量
        result.set("cartNumber",cartService.countLocal());
        //购物车当前总价值
        result.set("totalPrice",cartService.getTotalPrice());
        return ApiResult.ok(result);
    }
}
